package com.hkts.naturalgas.base.controller;


import com.hkts.naturalgas.base.entity.HktsTailGas;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.hkts.naturalgas.utils.CustomPageUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hkts.naturalgas.utils.Result;
import com.hkts.naturalgas.utils.ResultUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hkts.naturalgas.base.service.IHktsWasteGasService;
import com.hkts.naturalgas.base.entity.HktsWasteGas;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.time.ZoneId;
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 焙烧废气
 * @author yck
 * @since 2021-05-31
 */
@RestController
@RequestMapping("/instrumentdata/hktsWasteGas")
public class HktsWasteGasController {
    private final Logger logger = LoggerFactory.getLogger(HktsWasteGasController.class);

    @Autowired
    public IHktsWasteGasService iHktsWasteGasService;

    /**
     * 分页查询数据
     *
     * @param page         分页信息
     * @param hktsWasteGas 查询条件
     * @return
     */
    @ResponseBody
    @GetMapping("/getHktsWasteGasPageList")
    public Result<Object> getHontUserList(Page<HktsWasteGas> page, HktsWasteGas hktsWasteGas) {
        return ResultUtil.data(CustomPageUtils.getCustomPage(iHktsWasteGasService.selectPage(page, hktsWasteGas)));
    }

    /**
     * 保存和修改公用的
     *
     * @param hktsWasteGas 传递的实体
     * @return saveOrUpdate
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/hktsWasteGasSave")
    public Result<Object> hktsWasteGasSave(HktsWasteGas hktsWasteGas) {
        iHktsWasteGasService.saveOrUpdate(hktsWasteGas);
        return ResultUtil.success("保存成功");
    }

    /**
     * 根据id获取对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/hktsWasteGasGetById")
    public Result<Object> hktsWasteGasGetById(String id) {
        HktsWasteGas byId = iHktsWasteGasService.getById(id);
        return ResultUtil.data(byId);
    }

    /**
     * 根据id删除对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/hktsWasteGasDelete")
    public Result<Object> hontUserDelete(String id) {
        iHktsWasteGasService.removeById(id);
        return ResultUtil.success("删除成功");
    }

    /**
     * 批量删除对象
     *
     * @param item 实体集合ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/hktsWasteGasBatchDelete")
    public Result<Object> deleteBatchIds(String item) {
        List<String> ids = (List<String>) JSON.parse(item);
        iHktsWasteGasService.removeByIds(ids);
        return ResultUtil.success("删除成功");
    }
    @GetMapping("/getPageByTime")
    public Result<Object> getPageByTime(Page<HktsWasteGas> page, String startDate, String endDate){
        return ResultUtil.data(CustomPageUtils.getCustomPage(iHktsWasteGasService.selectPageByTime(page,startDate,endDate)));
    }
    /** 功能描述:
     *数据导入
     * @return:
     * @Author: 杨春科
     * @Date: 2021-05-31 11:17
     * @Description //TODO
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/importWasteGas")
    public Result<Object> importWasteGas(String url) throws Exception {
        File excelFile = new File(url);
        HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(excelFile));
        HSSFSheet sheet = wb.getSheetAt(0);
        HSSFRow row = sheet.getRow(2);
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (int i = sheet.getLastRowNum(); i >3; i--){
            HSSFRow row1 = sheet.getRow(i);
            HktsWasteGas gas=new HktsWasteGas();
            row1.getCell(0).getStringCellValue();
            gas.setTime(format.parse(row1.getCell(0).getStringCellValue()).toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
            row1.getCell(1).setCellType(CellType.STRING);
            float v = Float.parseFloat(row1.getCell(1).getStringCellValue());
            gas.setA00000Cou( v);
            row1.getCell(14).setCellType(CellType.STRING);
            float v1 = Float.parseFloat(row1.getCell(14).getStringCellValue());
            gas.setA19001Avg(v1);
            row1.getCell(11).setCellType(CellType.STRING);
            float v2= Float.parseFloat(row1.getCell(11).getStringCellValue());
            gas.setA21002Avg(v2);
            row1.getCell(5).setCellType(CellType.STRING);
            float v3= Float.parseFloat(row1.getCell(5).getStringCellValue());
            gas.setA21026Avg(v3);
            row1.getCell(2).setCellType(CellType.STRING);
            float v4= Float.parseFloat(row1.getCell(2).getStringCellValue());
            gas.setA34013Avg(v4);
            gas.setStopFlag(row1.getCell(15).getStringCellValue());
            iHktsWasteGasService.importWasteGas(gas);
        }
        return ResultUtil.success("数据导入成功！");
    }

    /** 功能描述:
     *数据导入
     * @return:
     * @Author: 杨春科
     * @Date: 2021-05-31 11:17
     * @Description //TODO
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/importOnTimeWasteGas")
    public Result<Object> importOnTimeWasteGas(String url) throws Exception {
        File excelFile = new File(url);
        HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(excelFile));
        HSSFSheet sheet = wb.getSheetAt(0);
        HSSFRow row = sheet.getRow(2);
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (int i = sheet.getLastRowNum()-1; i >=3; i--){
            HSSFRow row1 = sheet.getRow(i);
            HktsWasteGas gas=new HktsWasteGas();
            row1.getCell(0).getStringCellValue();
            gas.setTime(format.parse(row1.getCell(0).getStringCellValue()).toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
            row1.getCell(1).setCellType(CellType.STRING);
            float v = Float.parseFloat(row1.getCell(1).getStringCellValue());
            gas.setA00000Cou( v);
            row1.getCell(10).setCellType(CellType.STRING);
            float v1 = Float.parseFloat(row1.getCell(10).getStringCellValue());
            gas.setA19001Avg(v1);
            row1.getCell(8).setCellType(CellType.STRING);
            float v2= Float.parseFloat(row1.getCell(8).getStringCellValue());
            gas.setA21002Avg(v2);
            row1.getCell(4).setCellType(CellType.STRING);
            float v3= Float.parseFloat(row1.getCell(4).getStringCellValue());
            gas.setA21026Avg(v3);
            row1.getCell(2).setCellType(CellType.STRING);
            float v4= Float.parseFloat(row1.getCell(2).getStringCellValue());
            gas.setA34013Avg(v4);
            gas.setStopFlag(row1.getCell(15).getStringCellValue());
            iHktsWasteGasService.importOnTimeWasteGas(gas);
        }
        return ResultUtil.success("数据导入成功！");
    }
}
